package com.cn.ccp.action.course;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseIssueBean;
import com.cn.ccp.util.CCPContants;

/**
 * 教师回复留言
 */
public class TeacherCourseIssueAction extends BaseAction {
	private Log log = LogFactory.getLog(TeacherCourseIssueAction.class);
	private String content ;
	private String courseID  ;
	List<CourseIssueBean> questionList ;
	
	private String courseName ;
	
	private String currData ;
	
	private String currQuestionId ;
	private String currQuestionStudentName ;
	private String currQuestionStudentId ;
	
	public TeacherCourseIssueAction(){
		setPageSize(5);
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String cid = varMap.get("id");
		if(cid != null  && !cid.equals("")){
			courseID = cid ;
			courseName = getCourseService().getBeanById(courseID).getCourse_name();
			this.content= "";
			this.currQuestionId = "";
			getpagedetail();
		}
	}
	
	public void save(){
		content = content.trim();
		log.info(" --- currQuestionId = "+currQuestionId);
		if(currQuestionId == null  || currQuestionId.equals("") || currQuestionId.equals("0") ){
			showMsg("contentid", "online_answer_page_submit_teacher_fauire_1");
		}else if(content == null  || content.equals("")){
			showMsg("contentid", "online_answer_page_submit_content_null");
		}else{
			boolean flag = getCourseIssueService().saveTeacher(courseID, currQuestionStudentId, content, Integer.parseInt(currQuestionId));
			if(flag){
				showMsg("contentid", "online_answer_page_submit_teacher_success");
				content = "";
				setCurrentPage(1);
			}else{
				showMsg("contentid", "online_answer_page_submit_teacher_fauire");
			}
		}
		getpagedetail();
	}
	
	public void delAnwser(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		currQuestionId = varMap.get("cqid");
		getCourseIssueService().delAnswerById(currQuestionId);
		getpagedetail();
	}
	
	public void selectReplyStudent(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		currQuestionId = varMap.get("cqid");
		currQuestionStudentName = varMap.get("studentName");
		currQuestionStudentId = varMap.get("studentId");
		content = "";
		getpagedetail();
	}
	
	@Override
	public void getpagedetail() {
		setItemCount(getCourseIssueService().getTeacherCount(courseID));
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		if(start < 0)
			start = 0 ;
		log.info("start = "+start);
		log.info("pagesize = "+pagesize);
		log.info("courseID = "+courseID);
		questionList = getCourseIssueService().getTeacherCourseIssueListForStudentQuestion(courseID, "1" , start, pagesize);
		if(questionList.size() == 0)
			return;
		log.info("questionList.size() = "+questionList.size());
		StringBuffer sql = new StringBuffer();
		for(CourseIssueBean b:questionList){
			sql.append(b.getId());
			sql.append(",");
		}
		String s = sql.toString().substring(0,sql.toString().length() - 1);
		log.info(s);
		List<CourseIssueBean> answerList = getCourseIssueService().getAnswerById(s);
		log.info("answerList.size() = "+answerList.size());
		for (int i = 0; i < questionList.size(); i++) {
			CourseIssueBean qbean = questionList.get(i);
			for(CourseIssueBean abean : answerList){
				if(abean.getIssue_id().equals(""+qbean.getId())){
					qbean.getCcList().add(abean);
					questionList.set(i, qbean);
				}
			}
		}
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getCourseID() {
		return courseID;
	}

	public void setCourseID(String courseID) {
		this.courseID = courseID;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public List<CourseIssueBean> getQuestionList() {
		return questionList;
	}

	public void setQuestionList(List<CourseIssueBean> questionList) {
		this.questionList = questionList;
	}

	public String getCurrData() {
		Date date = new Date() ;
		String time = CCPContants.sdf3.format(date) ;
		return time;
	}
	public void setCurrData(String currData) { 
		this.currData = currData;
	}

	public String getCurrQuestionId() {
		return currQuestionId;
	}

	public void setCurrQuestionId(String currQuestionId) {
		this.currQuestionId = currQuestionId;
	}

	public String getCurrQuestionStudentName() {
		return currQuestionStudentName;
	}

	public void setCurrQuestionStudentName(String currQuestionStudentName) {
		this.currQuestionStudentName = currQuestionStudentName;
	}

	public String getCurrQuestionStudentId() {
		return currQuestionStudentId;
	}

	public void setCurrQuestionStudentId(String currQuestionStudentId) {
		this.currQuestionStudentId = currQuestionStudentId;
	}
	
}
